<?php

/* 公共方法 */

/**
 * 通用化API数据格式输出
 * @param  [string] $code       [状态码]
 * @param  [string] $message    [信息提示]
 * @param  [string] $data       [数据]
 * @param  [string] $httpStatus [Http状态码]
 * @return [mixed]              [数据格式]
 */
function show($code, $message = 'error', $data = [], $httpStatus = 200)
{
    $result = [
        'code' => $code,
        'message' => $message,
        'data' => $data
    ];
    return json($result, $httpStatus);
}


// 生成订单号
function buildOrderNum($payType = '')
{
    return $payType . date('YmdHis').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}

/**
 * 打印函数
 * @param  [mixed] $var [变量]
 * @return [mixed]      [格式化数据]
 */
function p($var)
{
    echo '<pre>' . htmlspecialchars(print_r($var, true)) . '</pre>';
}

/**
 * Curl远程请求
 * @param  [string] $url    [请求地址]
 * @param  [array]  $param  [POST参数]
 * @param  [boolen] $json   [Json]     [微信41005错误，使用false]
 * @return [mixed]          [请求结果]
 */
function curl($url, $param = [], $json = true)
{
    $curl = curl_init();                                    // 初始化
    curl_setopt($curl, CURLOPT_URL, $url);                  // 设置请求地址
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);       // 设置文件流返回
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);      // 证书来源检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);      // 检查SSL加密算法

    if (count($param)) {
        if ($json == true) {
            $param = json_encode($param);
        }
        curl_setopt($curl, CURLOPT_POST, true);             // 设置POST方式
        curl_setopt($curl, CURLOPT_POSTFIELDS, $param);     // 设置POST参数
    }

    $result = curl_exec($curl);                             // 执行请求
    if ($json == true) {
        $result = json_decode($result, true);               // 解析数据
    }
    curl_close($curl);                                      // 释放资源
    return $result;
}

function curl_post($url , $data=array())
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    // POST数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // 把post的变量加上
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;

}

function export_excel($fileName, $tileArray = [], $dataArray = [])
{
    ini_set('memory_limit', '512M');
    ini_set('max_execution_time', 0);
    // ob_end_clean();
    ob_start();
    header("Content-Type: text/csv");
    header("Content-Disposition:attachment;filename=" . $fileName);
    $fp = fopen('php://output', 'w');
    fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));// 转码 防止乱码(比如微信昵称)
    fputcsv($fp, $tileArray);
    $index = 0;
    foreach ($dataArray as $item) {
        if ($index == 1000) {
            $index = 0;
            ob_flush();
            flush();
        }
        $index++;
        fputcsv($fp, $item);
    }
    ob_flush();
    flush();
    ob_end_clean();
}

function get_stay_time($timestamp, $is_hour = 1, $is_minutes = 1)
{
    if(empty($timestamp) || $timestamp <= 60) {
        return false;
    }

    $time = time();
    $remain_time = $time - $timestamp;

    $day = floor($remain_time / (3600*24));
    $day = $day > 0 ? $day.'天' : '';
    $hour = floor(($remain_time % (3600*24)) / 3600);
    $hour = $hour > 0 ? $hour.'小时' : '';
    if($is_hour && $is_minutes) {
        $minutes = floor((($remain_time % (3600*24)) % 3600) / 60);
        $minutes = $minutes > 0 ? $minutes.'分' : '';
        return $day.$hour.$minutes;
    }

    if($hour) {
        return $day.$hour;
    }
    return $day;
}

/**
 * 根据经纬度计算距离
 * 单位为公里
 * @param $lat1
 * @param $lng1
 * @param $lat2
 * @param $lng2
 * @return float
 */
function getDistance($lat1, $lng1, $lat2, $lng2)
{
    $lat1 = deg2rad($lat1);
    $lat2 = deg2rad($lat2);
    $lng1 = deg2rad($lng1);
    $lng2 = deg2rad($lng2);
    $one = $lat1 - $lat2;
    $two = $lng1 - $lng2;
    $s = 2 * asin(sqrt(pow(sin($one / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($two / 2), 2))) * 6371;
    return round($s, 2);
}
